package pqt;

import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

public class AgregarBalanza  extends JFrame {

	BaseDeDatos db = BaseDeDatos.getInstance();
	final JTextField modelo;
	final JTextField descripcion;
	final JTextField codigo;
	final JButton btnAbrir = new JButton("...");
	final JPanel panelAgregarBalanza = new JPanel();
	JLabel lblModelo = new JLabel("Modelo:");
	final JSpinner pesoMinimo = new JSpinner();
	final JSpinner pesoMedio = new JSpinner();
	final JSpinner pesoMaximo = new JSpinner();
	JPanel panelCentral = new JPanel();
	
	
	public AgregarBalanza(JPanel panelAgregarBalanza) {
		
		panelAgregarBalanza.setLayout(null);
		panelCentral.setLayout(null);
		panelCentral.setBounds(300, 50, 800, 500);
		panelCentral.setBorder(new TitledBorder(new LineBorder(new Color(51, 153, 255)), "Agregar Balanza", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelAgregarBalanza.add(panelCentral);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(panelCentral.getX()+panelCentral.getWidth()-89, panelCentral.getY()+panelCentral.getHeight()+5, 89, 23);
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//tabbedPane.remove(panelAgregarBalanza);
			}
		});
		panelAgregarBalanza.add(btnCancelar);
		
		
		
		//-----------------------------------------Modelo-----------------------------------------------------
		
		
		lblModelo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblModelo.setBounds(50, 50, 59, 23);
		panelCentral.add(lblModelo);
		
		modelo = new JTextField();
		modelo.setBounds(150, 53, 215, 23);
		panelCentral.add(modelo);
		modelo.addKeyListener(new KeyAdapter() {
			 public void keyTyped(KeyEvent e) {
				 
				 if( modelo.getText().length()>50) {
					 e.consume();
				 }
			 }
		 });
		modelo.setColumns(10);
		
		
		//-----------------------------------------Descripcion--------------------------------------------------
			
		JLabel lblDescripcion = new JLabel("Descripcion:");
		lblDescripcion.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDescripcion.setBounds(50, 100, 89, 23);
		panelCentral.add(lblDescripcion);
		
		descripcion = new JTextField();
		descripcion.setBounds(150, 103, 215, 23);
		panelCentral.add(descripcion);
		descripcion.addKeyListener(new KeyAdapter() {
			 public void keyTyped(KeyEvent e) {
				 
				 if( descripcion.getText().length()>50) {
					 e.consume();
				 }
			 }
		 });
		descripcion.setColumns(10);
		
		//-----------------------------------------e1---------------------------------------------------------
		
		JLabel lblE1 = new JLabel("e1 (en g):");
		lblE1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblE1.setBounds(50, 150, 89, 23);
		panelCentral.add(lblE1);
		
		final JSpinner e1 = new JSpinner();
		e1.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		e1.setBounds(150, 153, 106, 20);
		panelCentral.add(e1);
		
		//----------------------------------------e2--------------------------------------------------------
		
		JLabel lblE2 = new JLabel("e2 (en g): ");
		lblE2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblE2.setBounds(50, 200, 70, 23);
		panelCentral.add(lblE2);
		
		final JSpinner e2 = new JSpinner();
		e2.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		e2.setBounds(150, 203, 106, 20);
		panelCentral.add(e2);
		
		//-------------------------------Peso minimo---------------------------------
		JLabel lblPesoMnimo = new JLabel("Peso M\u00EDnimo:");
		lblPesoMnimo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPesoMnimo.setBounds(50, 250, 89, 23);
		panelCentral.add(lblPesoMnimo);
		
		
		pesoMinimo.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		pesoMinimo.setBounds(150, 253, 106, 20);
		panelCentral.add(pesoMinimo);
		
		//----------------------------------------------------------------
		
		JLabel lblPesoMedio = new JLabel("Peso Medio:");
		lblPesoMedio.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPesoMedio.setBounds(50, 300, 89, 23);
		panelCentral.add(lblPesoMedio);
		
		
		pesoMedio.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		pesoMedio.setBounds(150, 303, 106, 20);
		panelCentral.add(pesoMedio);
		
		//-----------------------Peso Maximo------------------------
		JLabel lblPesoMximo = new JLabel("Peso M\u00E1ximo:");
		lblPesoMximo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPesoMximo.setBounds(50, 350, 89, 23);
		panelCentral.add(lblPesoMximo);
		
		
		pesoMaximo.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		pesoMaximo.setBounds(150, 353, 106, 20);
		panelCentral.add(pesoMaximo);
		
		//-------------------------Codigo---------------
				
		JLabel lblNDeSerie = new JLabel("Codigo:");
		lblNDeSerie.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNDeSerie.setBounds(400, 50, 89, 20);
		panelCentral.add(lblNDeSerie);
		
		codigo = new JTextField();
		codigo.setColumns(10);
		codigo.setBounds(500, 53, 215, 23);
		codigo.addKeyListener(new KeyAdapter() {
			 public void keyTyped(KeyEvent e) {
				 
				 if( codigo.getText().length()>50) {
					 e.consume();
				 }
			 }
		 });
		panelCentral.add(codigo);
		
		//----------------------------------------Imagen---------------------------------------
		
		final JLabel lblCalibracion = new JLabel("Calibracion:");
		lblCalibracion.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCalibracion.setBounds(400, 100, 89, 20);
		panelCentral.add(lblCalibracion);
		
		final JTextField calibracion = new JTextField();
		calibracion.setColumns(10);
		
		calibracion.setBounds(500, 103, 215, 23);
		panelCentral.add(calibracion);
		
		calibracion.addKeyListener(new KeyAdapter() {
			 public void keyTyped(KeyEvent e) {
				 
				 if( calibracion.getText().length()>50) {
					 e.consume();
				 }
			 }
		 });
				
				
		
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.setBounds(btnCancelar.getX()-100, panelCentral.getY()+panelCentral.getHeight()+5, 89, 23);
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				boolean consultar = true;
				if(calibracion.getText().equalsIgnoreCase("") || descripcion.getText().equalsIgnoreCase("") || modelo.getText().equalsIgnoreCase("") || codigo.getText().equalsIgnoreCase("")   ) {
					
					JOptionPane.showMessageDialog(null,"<html><h3>Debe completar todos los campos</h3></html>", "Informacion",JOptionPane.ERROR_MESSAGE);
					consultar = false;
				}
				
				if(!chequearPesos() && consultar){
					
					JOptionPane.showMessageDialog(null,"<html><h3>Los Pesos Ingresados son incorrectos</h3></html>", "Informacion",JOptionPane.ERROR_MESSAGE);
					consultar = false;
				}
				
				if( ((int)e1.getValue()>(int)e2.getValue()) && consultar){
					
					JOptionPane.showMessageDialog(null,"<html><h3>E1 debe ser menor que E2</h3></html>", "Informacion",JOptionPane.ERROR_MESSAGE);
					consultar = false;
				}
				
				
				
				
				if(consultar) {
					Balanza balanza = new Balanza();
					
					balanza.setDescripcion(descripcion.getText());
					balanza.setCargaMinima(Integer.parseInt(pesoMinimo.getValue().toString()));
					balanza.setCargaLimite(Integer.parseInt(pesoMedio.getValue().toString()));
					balanza.setCargaMaxima(Integer.parseInt(pesoMaximo.getValue().toString()));
					balanza.setCodigo(codigo.getText());
					balanza.setE1(Integer.parseInt(e1.getValue().toString()));
					balanza.setE2(Integer.parseInt(e2.getValue().toString()));
					balanza.setModelo(modelo.getText());
					balanza.setCalibracion(calibracion.getText());
					
					try {
						if(db.insertarBalanza(balanza)){
							JOptionPane.showMessageDialog(null,"El modelo se ingreso correctamente", "Informacion",JOptionPane.INFORMATION_MESSAGE);
						}
					} catch (HeadlessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
				
			}
		});
		
		panelAgregarBalanza.add(btnAceptar);
		
	}
	
	private boolean chequearPesos() {

		if(pesoMaximo.getValue().toString().equalsIgnoreCase(pesoMedio.getValue().toString()))
			return false;
		
		if(pesoMaximo.getValue().toString().equalsIgnoreCase(pesoMinimo.getValue().toString()))
			return false;
		
		if(pesoMedio.getValue().toString().equalsIgnoreCase(pesoMinimo.getValue().toString()))
			return false;
		
		if( ((int)pesoMedio.getValue()<=(int)pesoMinimo.getValue()) || ((int)pesoMedio.getValue()>=(int)pesoMaximo.getValue()) )
			return false;
		
		if( ((int)pesoMinimo.getValue()>=(int)pesoMedio.getValue()) || ((int)pesoMinimo.getValue()>=(int)pesoMaximo.getValue()) )
			return false;
		
		return true;
	}

	
}
